/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.pc9.pswing.components.filebrowser;

import br.com.pc9.pswing.util.SystemUtilsOS;
import br.com.pc9.pswing.util.UserFolders;
import br.com.pc9.pswing.util.WindowsUtil;
import java.io.File;
import javax.swing.Icon;

/**
 *
 * @author paulocanedo
 */
public class PFileView {

    private File file;
    private String text;

    public PFileView(File file) {
        this.file = file;
    }

    public PFileView(File file, String text) {
        this.file = file;
        this.text = text;
    }

    public File getFile() {
        return file;
    }

    public String getText() {
        if (text != null) {
            return text;
        }
        if (file.getParentFile() == null) {
            String apath = file.getAbsolutePath();
            if (apath.equals(WindowsUtil.getWindowsDrive())) {
                return "windows (" + apath + ")";
            }
            return apath;
        }
        return file.getName();
    }

    public Icon getIcon(int size) {
        if (file == null) {
            return null;
        }

        try {
            if (file.equals(SystemUtilsOS.getUserHomeAsFile())) {
                return SystemUtilsOS.getSystemIcon("places", "user-home", size);
            }
            UserFolders userFolders = SystemUtilsOS.getUserFolders();
            if (file.equals(userFolders.getDocumentFolder())) {
                return SystemUtilsOS.getSystemIcon("places", "folder-documents", size);
            } else if (file.equals(userFolders.getDownloadsFolder())) {
                return SystemUtilsOS.getSystemIcon("places", "folder-download", size);
            } else if (file.equals(userFolders.getPicturesFolder())) {
                return SystemUtilsOS.getSystemIcon("places", "folder-pictures", size);
            } else if (file.equals(userFolders.getVideosFolder())) {
                return SystemUtilsOS.getSystemIcon("places", "folder-videos", size);
            } else if (file.equals(userFolders.getPublicShareFolder())) {
                return SystemUtilsOS.getSystemIcon("places", "folder-publicshare", size);
            } else if (file.equals(userFolders.getTemplateFolder())) {
                return SystemUtilsOS.getSystemIcon("places", "folder-templates", size);
            } else if (file.equals(userFolders.getMusicFolder())) {
                return SystemUtilsOS.getSystemIcon("places", "folder-music", size);
            } else if (file.equals(userFolders.getDesktopFolder())) {
                return SystemUtilsOS.getSystemIcon("places", "user-desktop", size);
            }


//            for (PFileView fileview : SystemUtilsOS.getFavoritePlaces()) {
//                if (fileview.getFile().equals(file)) {
//                    return SystemUtilsOS.getSystemIcon("places", "user-bookmarks", size);
//                }
//            }
        } catch (Exception ex) {
        }

        if (file.isDirectory()) {
            return SystemUtilsOS.getSystemIcon("places", "folder", size);
        } else {
            return SystemUtilsOS.getSystemIcon("mimetypes", "gnome-fs-regular", size);
        }
    }

    @Override
    public String toString() {
        return getText();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final PFileView other = (PFileView) obj;
        if (this.file != other.file && (this.file == null || !this.file.equals(other.file))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 97 * hash + (this.file != null ? this.file.hashCode() : 0);
        return hash;
    }
}
